//
//  PictureTableViewCell.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/19.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "PictureTableViewCell.h"
#import "PictureModel.h"
#import "UIImageView+WebCache.h"


#define kTextFont [UIFont systemFontOfSize:14]

@interface PictureTableViewCell ()

/**用户名label*/
@property (nonatomic, strong) UILabel *nameLabel;

/**用户名头像label*/
@property (nonatomic, strong) UIImageView *iconImageView;

/**时间label*/
@property (nonatomic, strong) UILabel *timeLabel;

/**标题label*/
@property (nonatomic, strong) UILabel *titleLabel;

/**顶一下button*/
@property (nonatomic, strong) UIButton *loveButton;

/**踩一下button*/
@property (nonatomic, strong) UIButton *hateButton;

/**缩略图imageView*/
@property (nonatomic, strong) UIImageView *pictureImageView;

/**收藏button*/
@property (nonatomic, strong) UIButton *collectButton;

/**分享button*/
@property (nonatomic, strong) UIButton *shareButton;

@end


@implementation PictureTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = kTextFont;
        
        _iconImageView = [[UIImageView alloc] init];
        
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = kTextFont;
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.numberOfLines = 0;
        
        _loveButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _loveButton.titleLabel.font = kTextFont;
        
        _hateButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _hateButton.titleLabel.font = kTextFont;
        
        _pictureImageView  = [[UIImageView alloc] init];
        
        _collectButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        _shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        [self.contentView addSubview:_nameLabel];
        
        [self.contentView addSubview:_iconImageView];
        
        [self.contentView addSubview:_timeLabel];
        
        [self.contentView addSubview:_titleLabel];
        
        [self.contentView addSubview:_loveButton];
        
        [self.contentView addSubview:_hateButton];
        
        [self.contentView addSubview:_pictureImageView];
        
        [self.contentView addSubview:_collectButton];
        
        [self.contentView addSubview:_shareButton];

    }
    return self;
}

//子控件布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat margin = 10;
    
    self.iconImageView.frame = CGRectMake(margin, margin, 40, 40);
   
    
    self.nameLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImageView.frame)+margin, CGRectGetMinY(self.iconImageView.frame), 110, 30);
    
    self.timeLabel.frame = CGRectMake(CGRectGetMinX(self.nameLabel.frame), CGRectGetMaxY(self.nameLabel.frame), 110, 20);
    
    
    self.titleLabel.frame = CGRectMake(CGRectGetMinX(self.iconImageView.frame), CGRectGetMaxY(self.iconImageView.frame) + margin + 20, 160, 60);
    
    
    self.collectButton.frame = CGRectMake(CGRectGetMinX(self.iconImageView.frame), CGRectGetMaxY(self.titleLabel.frame) + margin , 70, 30);
    [self.collectButton setImage:[UIImage imageNamed:@"iconfont-shoucang-1"] forState:(UIControlStateNormal)];
    [self.collectButton addTarget:self action:@selector(collectButtonClick) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.shareButton.frame = CGRectMake(CGRectGetMaxX(self.collectButton.frame) + margin, CGRectGetMinY(self.collectButton.frame) , 70, 30);
    [self.shareButton setImage:[UIImage imageNamed:@"iconfont-fenxiang"] forState:(UIControlStateNormal)];
    [self.shareButton addTarget:self action:@selector(shareButtonClick) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    self.loveButton.frame = CGRectMake(CGRectGetMinX(self.iconImageView.frame), CGRectGetMaxY(self.collectButton.frame) + margin, 70, 30);
    
    [self.loveButton setImage:[UIImage imageNamed:@"iconfont-zan(2)"] forState:(UIControlStateNormal)];
    [self.loveButton setImage:[UIImage imageNamed:@"iconfont-zan(1)"] forState:(UIControlStateSelected)];
    [self.loveButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.loveButton addTarget:self action:@selector(loveButtonClick) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    self.hateButton.frame = CGRectMake(CGRectGetMinX(self.shareButton.frame), CGRectGetMaxY(self.shareButton.frame) + margin, 70, 30);
    [self.hateButton setImage:[UIImage imageNamed:@"iconfont-iconfontcai(2)"] forState:(UIControlStateNormal)];
    [self.hateButton setImage:[UIImage imageNamed:@"iconfont-iconfontcai(1)"] forState:(UIControlStateSelected)];
    [self.hateButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.hateButton addTarget:self action:@selector(hateButtonClick) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    self.pictureImageView.frame = CGRectMake(CGRectGetMaxX(self.titleLabel.frame) + margin, CGRectGetMinY(self.iconImageView.frame), 185, 185);

}

- (void)setPictureModel:(PictureModel *)pictureModel
{
    _pictureModel = pictureModel;
    
    [self.iconImageView setImageWithURL:[NSURL URLWithString:pictureModel.profile_image]];
    
    self.nameLabel.text = pictureModel.name;
    
    self.timeLabel.text = [NSString stringWithFormat:@"%@",[pictureModel.create_time substringWithRange:NSMakeRange(5, 11)]];
    
    self.titleLabel.text = pictureModel.text;
    
    [self.loveButton setTitle:[NSString stringWithFormat:@"顶 %@",pictureModel.love] forState:(UIControlStateNormal)];
    
    [self.hateButton setTitle:[NSString stringWithFormat:@"踩 %@",pictureModel.hate] forState:(UIControlStateNormal)];
    
    [self.pictureImageView setImageWithURL:[NSURL URLWithString:pictureModel.gifFistFrame]];
    
}

#pragma mark --- 收藏按钮点击事件
- (void)collectButtonClick
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您还没有登录，不能使用收藏功能，是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"前去登录", nil];
    [alertView show];
}

#pragma mark --- 分享按钮点击事件
- (void)shareButtonClick
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"第三方登陆还没有实现" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
}

#pragma mark --- 顶一下按钮点击事件
- (void)loveButtonClick
{
    if (self.loveButton.selected == NO) {
        self.loveButton.selected = YES;
    }else{
        self.loveButton.selected = NO;
    }
}

#pragma mark --- 踩一下按钮点击事件
- (void)hateButtonClick
{
    self.hateButton.selected = self.hateButton.selected ? NO : YES;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
